
<template>
  <div class="pageDetail">
    <el-form
      ref="formRef"
      class="form_item_w2"
      :model="pageForm"
      :rules="rules"
      label-width="130px"
    >
      <el-form-item label="被评教师" prop="evalTeacherName">
        <el-select
          v-model="pageForm.evalTeacherName"
          filterable
          remote
          reserve-keyword
          placeholder="请输入关键词"
          :remote-method="remoteMethod"
          :loading="loading"
          :disabled="disabled"
          @change="setValTeacher($event)"
        >
          <el-option
            v-for="item in userList"
            :key="item.userId"
            :label="item.userName"
            :value="item.userId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="评价教师" prop="teacherName">
        <el-select
          v-model="pageForm.teacherName"
          filterable
          remote
          reserve-keyword
          placeholder="请输入关键词"
          :remote-method="remoteMethod"
          :loading="loading"
          :disabled="disabled"
          @change="setTeacher($event)"
        >
          <el-option
            v-for="item in userList"
            :key="item.userId"
            :label="item.userName"
            :value="item.userId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所在部门" prop="deptId">
        <TheDeptSelect v-model="pageForm.deptId" :modelValue="pageForm.deptId" deptType="42" placeholder="请选择所属学院"  :disabled="disabled" @update:modelValue="pageForm.deptId = $event"/>
      </el-form-item>
      <el-form-item label="教学内容专业性" prop="scoreA">
        <el-input-number 
          v-model="pageForm.scoreA" 
          :min="1" 
          :max="10" 
          label="请输入1-10分" 
          :disabled="disabled"
          @change="calculateTotalScore()"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="实训指导规范性" prop="scoreB">
        <el-input-number 
          v-model="pageForm.scoreB" 
          :min="1" 
          :max="10" 
          label="请输入1-10分" 
          :disabled="disabled"
          @change="calculateTotalScore()"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="课程创新参与度" prop="scoreC">
        <el-input-number 
          v-model="pageForm.scoreC" 
          :min="1" 
          :max="10" 
          label="请输入1-10分" 
          :disabled="disabled"
          @change="calculateTotalScore()"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="师德师风表率" prop="scoreD">
        <el-input-number 
          v-model="pageForm.scoreD" 
          :min="1" 
          :max="10" 
          label="请输入1-10分" 
          :disabled="disabled"
          @change="calculateTotalScore()"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="校企合作协同力" prop="scoreE">
        <el-input-number 
          v-model="pageForm.scoreE" 
          :min="1" 
          :max="10" 
          label="请输入1-10分" 
          :disabled="disabled"
          @change="calculateTotalScore()"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="团队教研贡献" prop="scoreF">
        <el-input-number 
          v-model="pageForm.scoreF" 
          :min="1" 
          :max="10" 
          label="请输入1-10分"
          :disabled="disabled"
          @change="calculateTotalScore()"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="学生管理协作" prop="scoreG">
        <el-input-number 
          v-model="pageForm.scoreG" 
          :min="1" 
          :max="10" 
          label="请输入1-10分" 
          :disabled="disabled"
          @change="calculateTotalScore()"
        ></el-input-number>
      </el-form-item>
      
      <el-form-item label="教学资源共享性" prop="scoreH">
        <el-input-number 
          v-model="pageForm.scoreH" 
          :min="1" 
          :max="10" 
          label="请输入1-10分" 
          :disabled="disabled"
          @change="calculateTotalScore()"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="沟通协调效" prop="scoreI">
        <el-input-number 
          v-model="pageForm.scoreI" 
          :min="1" 
          :max="10" 
          label="请输入1-10分" 
          :disabled="disabled"
          @change="calculateTotalScore()"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="职业技能提升度" prop="scoreJ">
        <el-input-number 
          v-model="pageForm.scoreJ" 
          :min="1" 
          :max="10" 
          label="请输入1-10分" 
          :disabled="disabled"
          @change="calculateTotalScore()"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="应急事件处理" prop="emergencyHandling">
        <el-input
          v-model="pageForm.emergencyHandling" 
          label="请输入应急事件处理" 
          :disabled="disabled"
        ></el-input>
      </el-form-item>
      <el-form-item label="综合评分" prop="totalScore">
        <el-input-number 
          v-model="pageForm.totalScore" 
          :min="0" 
          label="综合得分" 
          :disabled="true"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="是否匿名" prop="isAnonymousId">
        <el-radio v-model="pageForm.isAnonymousId" :label="1">是</el-radio>
        <el-radio v-model="pageForm.isAnonymousId" :label="0">否</el-radio>
      </el-form-item>
      <el-form-item label="优势专长描述" class="item_w1">
        <el-input 
          type="textarea" 
          v-model="pageForm.strengths" 
          placeholder="请输入优势专长描述" 
          :disabled="disabled"
        ></el-input>
      </el-form-item>
      <el-form-item label="意见建议" class="item_w1">
        <el-input 
          type="textarea" 
          v-model="pageForm.suggestions" 
          placeholder="请输入意见建议" 
          :disabled="disabled"
        ></el-input>
      </el-form-item>
    </el-form>

    <div class="pageFooterBtn">
      <el-button @click="$router.back()">返回</el-button>
      <el-button type="primary" @click="handleSubmit()" v-if="['add','edit'].includes(action)" :loading="loading">
        提交
      </el-button>
    </div>
  </div>
</template>
<script>
import { rules } from './data'
import { evaluationAddApi, evaluationEditApi, evaluationByIdApi } from '@/api/intelligentEvaluation/index'
import { listUser } from "@/api/system/user";
import FileUpload from "@/components/FileUpload";
import TheDeptSelect from '@/componentsView/TheDeptSelect.vue';

export default {
  name: 'TeacherPeerDetail',
  components: { TheDeptSelect, FileUpload },
  data() {
    return {
      rules,
      userList: [],
      pageForm: {},
      loading:false,
      disabled:false,
      evaluationId: this.$route.query.evaluationId,
      action: this.$route.query.action
    }
  },
  async created() {
    if (this.evaluationId) this.getCoursePlanById()
  },
  methods: {
    async handleSubmit() {
      try {
        await this.$refs.formRef.validate()
        this.loading = true
        this.pageForm.evaluationTypeId = '194'
        let res = {}
        if (this.evaluationId) {
          delete this.pageForm.fileInfo
          res = await evaluationEditApi(this.pageForm)
        }else{
          res = await evaluationAddApi(this.pageForm)
        }
        if (res.code === 200) {
          this.$message.success(res.msg)
          this.$router.back()
        } else {
          this.$message.error(res.msg || '操作失败')
        }
      } catch (error) {
        console.log(error);
        
        this.$message.error('操作失败')
      } finally {
        this.loading = false
      }
    },

    async getCoursePlanById(){
      if (this.action === 'look') this.disabled = true
      evaluationByIdApi(this.evaluationId).then(res => {
        if (res.code === 200) {
          this.pageForm = res.data
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    remoteMethod(val){
      let params = { userName:val, pageNum:1 ,pageSize:10}
      listUser(params).then((response) => {
        if (response.code == 200) {
          this.userList = response.rows
        }else{
          this.$message.error(response.msg);
        }
      });
    },

    setTeacher(value){
      const selectedItem = this.userList.find(item => item.userId === value);
      if (selectedItem) {
        this.pageForm.teacherId = selectedItem.userId;
        this.pageForm.teacherName = selectedItem.userName
      }
    },
    setValTeacher(value) {
      const selectedItem = this.userList.find(item => item.userId === value);
      if (selectedItem) {
        this.pageForm.evalTeacherId = selectedItem.userId;
        this.pageForm.evalTeacherName = selectedItem.userName;
      }
    },
    calculateTotalScore() {
      // 定义参与计算的字段数组
      const scoreFields = [
        'scoreA',
        'scoreB',
        'scoreC',
        'scoreD',
        'scoreE',
        'scoreF',
        'scoreG',
        'scoreH',
        'scoreI',
        'scoreJ',
      ];

      // 过滤出已填写的有效分数（排除null/undefined）
      const validScores = scoreFields
        .map(field => this.pageForm[field])
        .filter(score => score !== null && score !== undefined);

      // 计算平均值（保留两位小数）
      if (validScores.length > 0) {
        const sum = validScores.reduce((total, score) => total + score, 0);
        this.pageForm.totalScore = sum;
      } else {
        // 若没有填写任何评分，综合得分为空
        this.pageForm.totalScore = null;
      }
    },
  },
}
</script>
<style scoped lang="scss">
::v-deep .el-select {
	width: 100% !important;
}
::v-deep .el-date-editor{
  width: 100% !important;
}
::v-deep .el-input-number{
  width: 100% !important;
}
</style>